#include "mystyleditemdelegate.h"

#include <QApplication>
#include <QStyle>

MyStyledItemDelegate::MyStyledItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}

void MyStyledItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                 const QModelIndex &index) const
{
    QStyleOptionViewItemV4 opt(option);
    initStyleOption(&opt, index);

    opt.state &= ~QStyle::State_HasFocus;
    if (opt.state & QStyle::State_Selected)
    {
        opt.palette.setBrush(QPalette::Highlight, QBrush(QColor(204, 232, 255)));
        opt.palette.setBrush(QPalette::HighlightedText,
                             qvariant_cast<QBrush>(index.data(Qt::ForegroundRole)));
    }
    else if (opt.state & QStyle::State_MouseOver)
    {
        opt.backgroundBrush = QBrush(QColor(229, 243, 255));
    }
    else
    {
        opt.backgroundBrush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole));
    }
    QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, NULL);
}
